
package com.sun.showcase.biz.testcase.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.testcase.dao.CaseModuleDao;
import com.sun.showcase.biz.testcase.service.CaseLibraryService;
import com.sun.showcase.biz.testcase.service.CaseModuleService;
import com.sun.showcase.client.testcase.domain.CaseModule;
import com.sun.showcase.client.testcase.query.CaseLibraryQuery;
import com.sun.showcase.client.testcase.query.CaseModuleQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("caseModuleService")
@Transactional
public class CaseModuleServiceImpl implements CaseModuleService{
	@Autowired
	private CaseModuleDao caseModuleDao;
	@Autowired
	private CaseLibraryService caseLibraryService;
	
	public void setCaseModuleDao(CaseModuleDao dao) {
		this.caseModuleDao = dao;
	}

	public DataGrid datagrid(CaseModuleQuery caseModuleQuery) {
		DataGrid j = new DataGrid();
		Pager<CaseModule> pager  = find(caseModuleQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<CaseModuleQuery> getQuerysFromEntitys(List<CaseModule> caseModules) {
		List<CaseModuleQuery> caseModuleQuerys = new ArrayList<CaseModuleQuery>();
		if (caseModules != null && caseModules.size() > 0) {
			for (CaseModule tb : caseModules) {
				CaseModuleQuery b = new CaseModuleQuery();
				BeanUtils.copyProperties(tb, b);
				caseModuleQuerys.add(b);
			}
		}
		return caseModuleQuerys;
	}

	private Pager<CaseModule> find(CaseModuleQuery caseModuleQuery) {
		Page<CaseModule> page = PageHelper.startPage(caseModuleQuery.getPage().intValue(), caseModuleQuery.getRows().intValue());
		List<CaseModule> list = caseModuleDao.findList(caseModuleQuery);
		Pager<CaseModule> pager = new Pager<CaseModule>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public CaseModule add(CaseModuleQuery caseModuleQuery) {
		CaseModule t = new CaseModule();
		BeanUtils.copyProperties(caseModuleQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
		caseModuleDao.save(t);
		caseModuleQuery.setId(t.getId());
		return t;
		
	}

	public void update(CaseModuleQuery caseModuleQuery) {
		CaseModule t = caseModuleDao.getById(caseModuleQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(caseModuleQuery, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	caseModuleDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(CaseModuleQuery caseModuleQuery) {
	    caseModuleDao.updatePart(caseModuleQuery);
	}

	public void delete(java.lang.Long[] ids) {
		caseModuleDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Long[] ids) {
		caseModuleDao.deletePt(ids);
	}

	public CaseModule get(CaseModuleQuery caseModuleQuery) {
		return caseModuleDao.getById(new Long(caseModuleQuery.getId()));
	}

	public CaseModule get(Long id) {
		return caseModuleDao.getById(id);
	}

	
	public List<CaseModuleQuery> listAll(CaseModuleQuery caseModuleQuery) {
	    List<CaseModule> list = caseModuleDao.findList(caseModuleQuery);
		List<CaseModuleQuery> listQuery =getQuerysFromEntitys(list) ;
		if(StringConstant.ONE.equals(caseModuleQuery.getQueryCount())){
			for(CaseModuleQuery mq : listQuery){
				CaseLibraryQuery caseLibraryQuery = new CaseLibraryQuery();
				caseLibraryQuery.setModuleCode(mq.getId());
				caseLibraryQuery.setActiveFlag(StringConstant.ONE);
				mq.setCaseCount(caseLibraryService.findPage_count(caseLibraryQuery));
			}
		}
		return listQuery;
	}
	
	
}
